package com.mall.entity;

import lombok.Data;
import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 商品实体
 * 
 * @author Mall Team
 */
@Data
public class Product {
    
    private Long id;
    private String name;
    private Long categoryId;
    private String brand;
    private BigDecimal price;
    private Integer stock;
    private String description;
    private String mainImage;
    private String images;  // JSON格式存储多张图片
    private Integer status;
    private LocalDateTime createTime;
    private LocalDateTime updateTime;
    
    // 关联对象
    private Category category;
    
    /**
     * 商品状态枚举
     */
    public enum Status {
        OFF_SHELF(0, "下架"),
        ON_SHELF(1, "上架");
        
        private final Integer code;
        private final String desc;
        
        Status(Integer code, String desc) {
            this.code = code;
            this.desc = desc;
        }
        
        public Integer getCode() {
            return code;
        }
        
        public String getDesc() {
            return desc;
        }
    }
    
    /**
     * 检查库存是否充足
     */
    public boolean isStockSufficient(Integer quantity) {
        return this.stock != null && this.stock >= quantity;
    }
    
    /**
     * 扣减库存
     */
    public void reduceStock(Integer quantity) {
        if (!isStockSufficient(quantity)) {
            throw new RuntimeException("库存不足");
        }
        this.stock -= quantity;
    }
}